package handler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"math/rand"
	"net/http"
	"socials/api/params"
	"socials/golbal"
	"socials/inits"
	"socials/modle"
	"socials/pkg"
	"strconv"
	"time"
)

func Register(c *gin.Context) {

}

func Sms(c *gin.Context) {
	var send params.SmsReq
	code := rand.Intn(9000) + 1000
	sms, err := pkg.SendSms(send.Mobile, strconv.Itoa(code))
	if err != nil {
		return
	}
	sprintf := fmt.Sprintf("%d:短信", send.Mobile)
	golbal.Rdb.Set(inits.Ctx, strconv.Itoa(code), sprintf, time.Minute*3)
	if *sms.Body.Code != "OK" {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 200,
			"msg":  *sms.Body.Message,
			"data": sms,
		})
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "短信发送成功",
	})

}
func Login(c *gin.Context) {

	req := params.LoginReq{}

	err := c.ShouldBind(&req)
	if err != nil {
		return
	}
	var user modle.User

	golbal.DB.Debug().Where("mobile=?", user.Mobile)

	handler, _ := pkg.TokenHandler(strconv.Itoa(int(user.ID)))

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": handler,
	})
}

func ContactAdd(c *gin.Context) {
	add := params.Contact{}
	if err := c.ShouldBind(&add); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	contact := modle.Contact{

		UserId:   add.UserId,
		Image:    add.Image,
		Nickname: add.Nickname,
		Status:   add.Status,
	}

	golbal.DB.Debug().Create(&contact)

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "添加好友成功",
	})
}

func MomentCreate(c *gin.Context) {

	red := params.Moment{}
	err := c.ShouldBind(&red)
	if err != nil {
		return
	}
	moment := modle.Moment{

		Name:    red.Name,
		Image:   red.Image,
		Content: red.Content,
	}
	golbal.DB.Debug().Create(&moment)

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "朋友圈发布成功",
	})

}
func Send(c *gin.Context) {

}

func Create(c *gin.Context) {

}
